package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.dao.ActionLogDao;
import cc.linker.steplink.crm.dao.PersonalCenterDao;
import cc.linker.steplink.crm.domain.UpdateBindCustomerPo;
import cc.linker.steplink.crm.merchant.util.Global;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.CustomerService;
import cc.linker.steplink.crm.service.ISyncService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * idp同步
 *
 * @author zhaomengwei
 */
@Slf4j
@RestController
@RequestMapping("/sync")
public class SyncController {

    @Autowired
    private ISyncService syncService;
    @Autowired
    private PersonalCenterDao personalCenterDao;

    @RequestMapping(value = {"/idp"})
    public void syncIdpUser(@RequestBody Map<String, Object> idpInfo) throws Exception {
        LOGGER.info("idp客户推送");
        if (idpInfo == null) {
            throw GlobalException.build("无有效数据");
        }
        syncService.analyse(idpInfo);
    }

    @RequestMapping(value = {"/idp/action"})
    public void syncIdpUserAction(@RequestBody Map<String, Object> data) throws Exception {
        LOGGER.info("idp客户行为推送");
        if (data == null) {
            throw GlobalException.build("无有效数据");
        }
        syncService.syncIdpUserAction(data);
    }

    @PostMapping("/third")
    public void third(@RequestBody Map<String, Object> map) throws GlobalException {
        LOGGER.info("三方客户信息推送");
        if (map == null) {
            throw GlobalException.build("无有效数据");
        }
        LOGGER.info("三方客户信息推送参数：" + JSON.toJSONString(map));
        syncService.third(map);
    }

    /**
     * @Description：同步微信小程序用户信息
     * @DateTime：2021/5/13 9:53
     * @Params： customerId : 用户编号
     * nickName ： 昵称
     * userIcon：  头像
     * orgId：机构
     * gender：性别
     * customerBirth：生日
     * province ：省份
     * city：城市
     * region ： 地区
     * wxappid：小程序ID
     * openId ： 用户openId
     * @Return：
     */
    @PostMapping("/syncWxMiniCustomer")
    public Map<String, Object> saveWxMiniCustomer(@RequestBody Map<String, Object> map) {
        if (map == null) {
            throw GlobalException.build("无有效数据");
        }
        LOGGER.info("微信小程序用户同步");
        return syncService.saveWxMiniCustomer(map);
    }

    /**
     * @Description：修改用户绑定
     * @DateTime：2021/5/17 20:33
     * @Params：
     * @Return：
     */
    @PostMapping("/updateBindCustomerId")
    public void updateBindCustomerId(@RequestBody UpdateBindCustomerPo updateBindCustomerPo) {
      this.syncService.updateBindCustomerId(updateBindCustomerPo);
    }
}
